#include <SDL/SDL.h>	//SDL_Xxxx

#include "graphics.h"
#include "infoBar.h"
#include "gameObjects.h"


//#define omakone  //muuten debuggeri ei toimi

#ifdef omakone
	#define INFOBAR "C:\\Tuotokset\\tankkipeli\\Debug\\infobar.bmp"
 	#define PROIMG "C:\\Tuotokset\\tankkipeli\\Debug\\proBig.bmp"
#else
	#define INFOBAR "infobar.bmp"
	#define PROIMG "proBig.bmp"
#endif

typedef struct info
{
	SDL_Rect box;
	SDL_Surface *graphics;
	
} info;

static info infoBar[3];

static SDL_Surface *scr, *pro, *bg;

// ammuksen kuvakartan jako
static SDL_Rect coords[]={ { 0, 0, 20, 20 }, { 20, 0, 20, 20 }, { 40, 0, 20, 20 }} ;

// infobarin grafiikoissa ammuksen kohta
static SDL_Rect ammoBox = { 110, 22, 25, 26 };

static struct Tankki *tanks;

void intInfoBar(SDL_Surface **screen, SDL_Surface **back, int numInfoBars , struct Tankki tankit[])
{
	int i;
	SDL_Surface *tmp;
	
	scr = *screen;
	bg = *back;
	
	tanks = tankit;
	
	loadImage(INFOBAR, &tmp, 0, 0, 0);
	
	for(i=0; i<3; i++)
	{
		infoBar[i].graphics = SDL_DisplayFormat(tmp);
	}
	SDL_FreeSurface(tmp);
	
	intFontDraw();
	
	loadImage(PROIMG, &pro, 0, 0, 0);
}

void createInfoBar(int player, int x, int y, int health, int fuel, int ammoType)	
{
	infoBar[player].box.x = x;
	infoBar[player].box.y = y;
	infoBar[player].box.w = infoBar[player].graphics->w;
	infoBar[player].box.h = infoBar[player].graphics->h;
	
	
	/*	
	//drawString(infoBar[i], tanks[i].nimi, 8, 2); 
	setHealthBar(player, health);
	setFuelBar(player, fuel);
	setAmmoType(player, ammoType);
	
	SDL_BlitSurface(infoBar[player].graphics, NULL, scr, &infoBar[player].box);
	SDL_UpdateRect(scr, infoBar[player].box.x, infoBar[player].box.y, infoBar[player].box.w, infoBar[player].box.h);
	*/
}

void setName(int player)
{
	drawString(bg, bg, tanks[player].nimi, infoBar[player].box.x+9  , infoBar[player].box.y+2);
}

void setHealthBar(int player, int health )  
{
	int i;
	if( SDL_MUSTLOCK(infoBar[player].graphics) )
		SDL_LockSurface(infoBar[player].graphics);
		
		for(i = 22; i < 33; i++)
		{
			drawHorLine(infoBar[player].graphics, 3, i, 100, 0xffcc99);
			drawHorLine(infoBar[player].graphics, 3, i, health, 0xff0000);
		}
	 
    if ( SDL_MUSTLOCK(infoBar[player].graphics) )
		SDL_UnlockSurface(infoBar[player].graphics);	
		
	drawIMGrect(infoBar[player].graphics, bg, infoBar[player].box.x+3, infoBar[player].box.y+22, 100, 11, 
											   infoBar[player].box.x+3, infoBar[player].box.y+22);
				
	//SDL_BlitSurface(infoBar[player].graphics, NULL, scr, &infoBar[player].box);
	//SDL_UpdateRect(scr, infoBar[player].box.x+3, infoBar[player].box.y+22, 100, 11 );
}


void setFuelBar(int player, int fuel)  
{
	int i;
	if( SDL_MUSTLOCK(infoBar[player].graphics) )
		SDL_LockSurface(infoBar[player].graphics);
		
		for(i = 37; i < 48; i++)
		{
			drawHorLine(infoBar[player].graphics, 3, i, 100, 0xffcc99);
			drawHorLine(infoBar[player].graphics, 3, i, fuel,0x669999);
		}
	 
    if ( SDL_MUSTLOCK(infoBar[player].graphics) )
		SDL_UnlockSurface(infoBar[player].graphics);
	
	drawIMGrect(infoBar[player].graphics, bg, infoBar[player].box.x+3, infoBar[player].box.y+37, 100, 11, 
											   infoBar[player].box.x+3, infoBar[player].box.y+37);	
	//SDL_BlitSurface(infoBar[player].graphics, NULL, scr, &infoBar[player].box);	
	//SDL_UpdateRect(scr, infoBar[player].box.x+3, infoBar[player].box.y+37, 100, 11 );	
}

void setAmmoType(int player, int ammoType)
{
	//SDL_FillRect(infoBar[player].graphics, &ammoBox, 0xffcc99);
	
	//SDL_BlitSurface(pro, &coords[ammoType],infoBar[player].graphics, &ammoBox);
	
	drawIMGrect(pro, bg, coords[ammoType].x,  coords[ammoType].y, coords[ammoType].w, coords[ammoType].h,  infoBar[player].box.x+ammoBox.x , infoBar[player].box.y+ammoBox.y);
		
	//SDL_BlitSurface(infoBar[player].graphics, NULL, scr, &infoBar[player].box);
	//SDL_UpdateRect(scr, infoBar[player].box.x+ammoBox.x, infoBar[player].box.y+ammoBox.y, ammoBox.w, ammoBox.h);
}

void updateInfoBars()
{
	int i;
	for(i=0; i<3; i++)
	{
		setName(i);
		setHealthBar(i, tanks[i].health);
		setFuelBar(i,tanks[i].speed); 
		setAmmoType(i, tanks[i].ammoType);
		
		
		SDL_BlitSurface(infoBar[i].graphics, NULL, bg, &infoBar[i].box);
		SDL_BlitSurface(bg, &infoBar[i].box, scr, &infoBar[i].box);
		SDL_UpdateRect(scr, infoBar[i].box.x, infoBar[i].box.y, infoBar[i].box.w, infoBar[i].box.h);
	}
}


